﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Win_Oracle
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MM0009_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void MM0101_Click(object sender, EventArgs e)
        {
            OpenForm(typeof(Form1));
        }

        /// <summary>
        /// 加载指定类型的窗体
        /// </summary>
        /// <param name="formtype">窗体类型</param>
        private void OpenForm(Type formtype)
        {
            Form form = this.MdiChildren.FirstOrDefault(n => n.GetType() == formtype);
            if (form == null)
            {
                object formobj = formtype.Assembly.CreateInstance(formtype.FullName);
                form = formobj as Form;
                OpenForm(form);
            }
            else
                form.Activate();
        }

        private void OpenForm(Form form)
        {
            form.MdiParent = this;

            form.WindowState = FormWindowState.Normal;

            //form.ShowIcon = false;
            form.ShowInTaskbar = false;
            form.Show();
        }

        private void OpenForm(string package, string formName)
        {
            try
            {
                string filePath = Path.Combine(Application.StartupPath, package);

                Assembly assembly = Assembly.LoadFile(filePath);
                object obj = assembly.CreateInstance(formName);
                Form f = obj as Form;
                OpenForm(f);
            }
            catch (FileNotFoundException fnfe)
            {
                //找不到要加载的文件
                throw fnfe;
            }
            catch (FileLoadException fle)
            {
                //无法加载的文件
                throw fle;
            }
            catch (BadImageFormatException bife)
            {
                //不是有效的File
                throw bife;
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.IsMdiContainer = true;
            this.StartPosition = FormStartPosition.CenterScreen;
        }

    }
}
